package com.atguigu.yygh.hosp.controller;


import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {

    // 注入
    private final HospitalSetService hospitalSetService;

    @Autowired
    public HospitalSetController(HospitalSetService hospitalSetService) {
        this.hospitalSetService = hospitalSetService;
    }

    // 1.查询医院设置表里面的所有信息
    @GetMapping("findAll")
    public Result findAllHospitalSet() {

        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok().data("list", list);
    }

    // 2.条件查询+分页
    @PostMapping("findPage/{current}/{limit}")
    public Result findPage(@RequestBody(required = false) HospitalSetQueryVo hospitalSetQuery,
                           @PathVariable("current") long current,
                           @PathVariable("limit") long limit) {
        // 1.创建page对象
        Page<HospitalSet> page = new Page<>(current, limit);

        // 2.调用方法实现分页
        Page<HospitalSet> page1 = hospitalSetService.findPage(page, hospitalSetQuery);

        return Result.ok().data("data", page1);
    }

    // 3.添加
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        // 设置状态 1 可以使用, 0不可以使用
        hospitalSet.setStatus(1);
        // 签名密钥
        Random random = new Random();
        String encrypt = MD5.encrypt(System.currentTimeMillis() + String.valueOf(random.nextInt(1000)));
        hospitalSet.setSignKey(encrypt);
        // 手动设置完成
        boolean save = hospitalSetService.save(hospitalSet);

        return save ? Result.ok() : Result.error().message("添加医院信息失败");

    }

    // 4.修改 根据id获取医院信息
    @GetMapping("getHospitalSetById/{id}")
    public Result getHospitalSetById(@PathVariable("id") String id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        return Result.ok().data("hospitalSet", hospitalSet);
    }


    // 5.进行更改
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean b = hospitalSetService.updateById(hospitalSet);

        return b ? Result.ok() : Result.error().message("修改失败");
    }

    // 6.批量删除
    @DeleteMapping("deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> idList) {

        // 参数是集合
        boolean b = hospitalSetService.removeByIds(idList);

        return b ? Result.ok() : Result.error().message("批量删除失败");

    }

    // 7.单个删除
    @DeleteMapping("deleteHospitalById/{id}")
    public Result deleteHospitalById(@PathVariable("id") String id) {
        boolean b = hospitalSetService.removeById(id);
        return b ? Result.ok() : Result.error().message("删除失败");
    }

    // 8.医院设置锁定和解锁 锁定我们就不能对接
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") Long id, @PathVariable("status") Integer status) {

        // 根据id查询医院的设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        hospitalSet.setStatus(status);

        boolean b = hospitalSetService.updateById(hospitalSet);
        return b ? Result.ok() : Result.error().message("修改状态失败");
    }

    // 9.发送签名的密钥
    @PutMapping("sendSignKey/{id}")
    public Result sendSignKey(@PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 密钥
        String signKey = hospitalSet.getSignKey();
        // 医院编号
        String hoscode = hospitalSet.getHoscode();
        // TODO 发送短信
        return Result.ok();
    }

}
